package com.xc.arr;

import java.util.Arrays;

public class Arrde {

    public static void main(String[] args) throws Exception {

//       int  nums[] ={3,2,2,3};
//       int  nums[] ={1,2,3,5,6};
//        int nums[] = {-7, -1, 0, 5, 6, 8};
//        int nums[] = {2,3,1,2,4,3};
        int nums[] = {5,1,3,5,10,7,4,9,2,8};
//        int i = removeElement(nums, 3);
//        nums = new int[i];
//        System.out.println(removeElement(nums, 3));

//        int[] retest = retest(nums, 3);
//        System.out.println(Arrays.toString(retest));

//        System.out.println(Arrays.toString(res(nums)));

//        System.out.println(len(nums, 15));
        System.out.println(Arrays.toString(generateMatrix(3)));
    }

    //螺旋矩阵



        public static int[][] generateMatrix(int n) {
            int l = 0, r = n - 1, t = 0, b = n - 1;
            int[][] mat = new int[n][n];
            int num = 1, tar = n * n;
            while(num <= tar){
                for(int i = l; i <= r; i++) mat[t][i] = num++; // left to right.
                t++;
                for(int i = t; i <= b; i++) mat[i][r] = num++; // top to bottom.
                r--;
                for(int i = r; i >= l; i--) mat[b][i] = num++; // right to left.
                b--;
                for(int i = b; i >= t; i--) mat[i][l] = num++; // bottom to top.
                l++;
            }
            return mat;
        }





    public static  int len(int[] nums,int val){
        int len=Integer.MAX_VALUE;

         int minLen=0;
        for (int i = 0; i < nums.length; i++) {
            int sum=0;
            for (int j = i; j < nums.length; j++) {
                sum+=nums[j];
                if (sum>=val){
                    minLen=j-i+1;

                    if (len >minLen){
                        len = minLen;
                    }
                    break;
                }

            }
        }

        return len==Integer.MAX_VALUE?0:len;
    }

    public static int[] res(int nums[]) {

        for (int i = 0; i < nums.length; i++) {
            nums[i] = nums[i] * nums[i];
        }
//        for (int i = 0; i < nums.length; i++) {
//            for (int j = i+1; j <nums.length; j++){
//                if (nums[i]>nums[j]){
//                    int temp=nums[i];
//                    nums[i]=nums[j];
//                    nums[j] = temp;
//                }
//            }
//
//        }

        for (int i = 0; i < nums.length - 1; i++) {
            int min = nums[i];
            int index = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (min > nums[j]) {
                    min = nums[j];
                    index = j;
                }
            }

            if (index != i) {
                nums[index] = nums[i];
                nums[i] = min;
            }

        }
        return nums;
    }

    public static int[] retest(int[] nums, int tar) {//返回数组长度
        int j = 0;
        for (int i = 0; i < nums.length; i++) {

            if (nums[i] != tar) {
                nums[j++] = nums[i];
            }
        }

        return nums;
    }

    public static int removeElement(int[] nums, int val) {
        int size = nums.length;
        for (int i = 0; i < size; i++) {
            if (nums[i] == val) { // 发现需要移除的元素，就将数组集体向前移动一位
                for (int j = i + 1; j < size; j++) {
                    nums[j - 1] = nums[j];
                }
                i--; // 因为下标i以后的数值都向前移动了一位，所以i也向前移动一位
                size--; // 此时数组的大小-1
            }
        }
        return size;

    }
}
